/*
 * AacRtpPacker.cpp
 *
 *  Created on: 2018年4月21日
 *      Author: zhengboyuan
 */

#include "AacRtpPacker.h"

#define AU_HEADER_SIZE  4

namespace av
{

AacRtpPacker::AacRtpPacker()
{
}

AacRtpPacker::~AacRtpPacker()
{
}

bool AacRtpPacker::open()
{
	return true;
}

void AacRtpPacker::pack(const MediaPacket& pkt, int maxSize, RtpPackerSink sink)
{
	maxSize -= AU_HEADER_SIZE;

	uint8_t* data = pkt.data;
	int size = pkt.size;

	uint8_t header[AU_HEADER_SIZE] = { 0x00, 0x10, 0x00, 0x00 };
	header[2] = (size & 0x1fe0) >> 5;
	header[3] = (size & 0x1f) << 3;

	if (size > maxSize)
	{
		while (size > maxSize)
		{
			m_buffer.clear();
			m_buffer.write(header, AU_HEADER_SIZE);
			m_buffer.write(data, maxSize);

			data += maxSize;
			size -= maxSize;

			RtpPacket pktOut;
			pktOut.data = m_buffer.data();
			pktOut.size = m_buffer.size();
			pktOut.ts = (uint32_t)pkt.pts;

			pktOut.mark = false;
			pktOut.pt = m_payload;

			sink(pktOut);
		}
	}

	m_buffer.clear();
	m_buffer.write(header, AU_HEADER_SIZE);
	m_buffer.write(data, size);

	RtpPacket pktOut;
	pktOut.data = m_buffer.data();
	pktOut.size = m_buffer.size();
	pktOut.ts = (uint32_t)pkt.pts;

	pktOut.mark = true;
	pktOut.pt = m_payload;

	sink(pktOut);

}

void AacRtpPacker::close()
{
	// pass
}


} /* namespace av */
